using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;

namespace BilSimser.SharePoint.Common.Controls
{
	/// <summary>
	/// This type is used to specify in what format this submit button will render.
	/// </summary>
	public enum OWSButtonType
	{
		Button,
		HyperLink
	}

	/// <summary>
	/// This control will cause the postback of sharepoint stuff. 
	/// </summary>
	public class OWSSubmitButton : WebControl, IPostBackEventHandler
	{
		#region properties

		/// <summary>
		/// Gets or sets the type of link
		/// </summary>
		public OWSButtonType ButtonType
		{
			get
			{
				if (ViewState["ButtonType"] == null)
					ButtonType = OWSButtonType.HyperLink;
				return (OWSButtonType) ViewState["ButtonType"];
			}
			set { ViewState["ButtonType"] = value; }
		}

		/// <summary>
		/// The display text of the button
		/// </summary>
		public string Text
		{
			get { return "" + ViewState["Text"]; }
			set { ViewState["Text"] = value; }
		}

		#endregion

		#region overridden methods

		/// <summary>
		/// Overrides the onload event, and emits the javascript.
		/// </summary>
		protected override void OnLoad (EventArgs e)
		{
			EmitProcessFormScript();
			base.OnLoad(e);
		}

		/// <summary>
		/// Renders the control
		/// </summary>
		/// <param name="writer">the html output write to which to render</param>
		protected override void Render (HtmlTextWriter writer)
		{
			switch (ButtonType)
			{
				case OWSButtonType.HyperLink:
					writer.Write(
						"<a class=\"{0}\" href=\"{1}\" >{2}</a>",
						CssClass,
						string.Format(
							"javascript:ProcessOwsForm('{0}', 'Click');",
							UniqueID
							),
						SPEncode.HtmlEncode(Text)
						);
					break;

				case OWSButtonType.Button:
					writer.Write(
						"<input type=\"button\" class=\"{0}\" onclick=\"{1}\" value=\"{2}\" />",
						CssClass,
						string.Format(
							"ProcessOwsForm('{0}', 'Click');",
							UniqueID
							),
						SPEncode.HtmlEncode(Text)
						);
					break;
			}
		}

		#endregion

		#region IPostBackEventHandler Members

		/// <summary>
		/// Causes the click event to fire
		/// </summary>
		/// <param name="eventArgument">this is the event argument</param>
		public void RaisePostBackEvent (string eventArgument)
		{
			OnClick(EventArgs.Empty);
		}

		#endregion

		#region events

		/// <summary>
		/// Fires when the submit button is clicked. 
		/// </summary>
		public event EventHandler Click;

		#endregion

		#region virtual methods

		/// <summary>
		/// Helper method for the onclick event
		/// </summary>
		protected virtual void OnClick (EventArgs e)
		{
			if (Click != null)
				Click(this, e);
		}

		#endregion

		#region helper methods

		/// <summary>
		/// Emits scripting to force the processing of the form script. 
		/// </summary>
		private void EmitProcessFormScript()
		{
			if (!Page.IsClientScriptBlockRegistered("OWSProcessFormScript"))
			{
				Page.RegisterStartupScript(
					"OWSProcessFormScript",
					"<script> \r\n" +
						"function ProcessOwsForm(ctl, argument) \r\n" +
						"{ \r\n" +
						"    if (frm.fPreviewMode) \r\n" +
						"    { \r\n" +
						"        var L_cantSave_Text = \"This form cannot be saved when previewing this page.\"; \r\n" +
						"        window.alert(L_cantSave_Text); \r\n" +
						"        return; \r\n" +
						"    } \r\n" +
						"    if (frm.FValidate(true)) \r\n" +
						"    { \r\n" +
						"        frm.FPostProcess(); \r\n" +
						"        __doPostBack(ctl, argument); \r\n" +
						"    } \r\n" +
						"} \r\n" +
						"</script>"
					);
				Page.GetPostBackEventReference(this);
			}
		}

		#endregion
	}


}